package com.filmatchs.filmatchs;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONArray;
import org.json.JSONException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.pm.FeatureInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

import com.filmatchs.R;
import com.filmatchs.utils.*;

public class PlayingMovies extends Activity {
	private static final String LOG_TAG = "Main";

	private List<Movie> movies = new ArrayList<Movie>();
	private ListAdapter adapter = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

		setContentView(R.layout.main);
		Log.i(LOG_TAG, "Tes berhasil!");

		setProgressBarIndeterminate(true);

		if (adapter == null) {
			new PlayingMoviesAsyncTask().execute();
		} else {
			setProgressBarIndeterminate(false);
		}
	}

	private class PlayingMoviesAsyncTask extends AsyncTask<Void, Void, Void> {

		@Override
		protected Void doInBackground(Void... params) {
			String jsonString = "";
			try {
				HttpClientManager rc = new HttpClientManager(getResources()
						.getString(R.string.SERVICE_URL)
						+ getResources().getString(R.string.MOVIES_PLAYING),
						HttpClientManager.METHOD_GET, "/format/json");

				jsonString = rc.getResult();
				Log.d(LOG_TAG, jsonString);

			} catch (ClientProtocolException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				JSONArray jsonArr = new JSONArray(jsonString);

				for (int i = 0; i < jsonArr.length(); i++) {
					/*
					 * TODO: DISINI DITAMBAH KONEKSI DAN SIMPAN KE
					 * CONTENTPROVIDER. KEMUDIAN DATA DARI JSON VERIFIKASI
					 * DENGAN ISI CONTENTPROVIDER, YANG BARU DIMASUKKAN KE
					 * CONTENTPROVIDER, DAN KE ARRAYLIST, DAN JIKA ADA LANGSUNG
					 * SIMPAN KE ARRAYLIST
					 */

					movies.add(new Movie(jsonArr.getJSONObject(i).getString(
							"id"), jsonArr.getJSONObject(i).getString("title"),
							jsonArr.getJSONObject(i).getString("image")));
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}

			return null;
		}

		@Override
		protected void onPostExecute(Void resultss) {

			adapter = new MovieAdapter(getApplicationContext(),
					R.layout.movie_item, movies);

			ListView movieList = (ListView) findViewById(R.id.list);

			movieList.setOnItemClickListener(new OnItemClickListener() {

				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					Movie c = (Movie) adapter.getItem(position);
					android.content.Intent i = new android.content.Intent(
							"com.filmatchs.TAMPIL_DETAIL");
					Log.d(LOG_TAG, c.getTitle() + c.getId());
					i.putExtra("id", c.getId());
					startActivity(i);
				}
			});

			movieList.setAdapter(adapter);
			setProgressBarIndeterminate(false);
		}

	}
}